package org.softmed.rest.server.core.handler.impls;

import org.softmed.rest.config.Handler;
import org.softmed.rest.server.core.handler.HandlerProcessParameters;
import org.softmed.rest.server.core.handler.IHandlerSolution;
import org.softmed.rest.server.core.handler.SpecificHandlerSolver;

public class JavaSolver implements SpecificHandlerSolver {

	private Class<?> type;
	IHandlerSolution javaHandler;

	@Override
	public boolean canSolve(Handler handler) {
		try {
			type = Class.forName(handler.getPath());
			if (IHandlerSolution.class.isAssignableFrom(type))
				return true;
		} catch (ClassNotFoundException e) {
			return false;
		}
		return true;
	}

	@Override
	public boolean isContinueProcessing() {
		return javaHandler.isContinueProcessing();
	}

	@Override
	public void solve(Handler handler, HandlerProcessParameters parameters)
			throws Throwable {
		try {
			javaHandler = (IHandlerSolution) type.newInstance();
			javaHandler.solve(handler, parameters);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public SpecificHandlerSolver getClone() {
		return new JavaSolver();
	}

}
